'use strict'

class user {

  constructor (mdb) {
    this.db = mdb
    this.tableName = 'says'
  }

  model () {
    return this.db.model(this.tableName)
  }
  
  makeId () {
    return `u_${Date.now().toString(16)}${parseInt(Math.random()*10000) + 11111}`
  }

  async insert(data){
    data.sayid = this.makeId()
    await this.model().insert(data)
    return data.sayid
  }

  async get(userid,groupid,sayid){
    let ulist = await this.model().where({userid,groupid,sayid}).select()
    return ulist.rows
  }

  async list () {
    let ulist = await this.model().limit(20).select()
    return ulist.rows
  }

  async delete(id){
    await this.model().where({sayid:id}).delete();
  }

  async update(data,sayid){
    await this.model().where({sayid}).update(data)
  }

}

module.exports = user
